package com.mf.utils;

import com.mf.entity.CodeHistory;
import com.mf.vo.StockGatherVo;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @Author: lmf
 * @Create: 2024/6/4 16:13
 * Description: 获取所股票资源集合
 */
public class StockGatherUtils {
    // 删除指定日期前的数据
    public static void deleteBeforeDate(StockGatherVo stockGatherVo, String date) {

        // 定义日期格式
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        // 将字符串转换为LocalDate对象
        LocalDate startDate = LocalDate.parse(date, formatter);
        for (int i = 0; i < stockGatherVo.getDate().length; i++) {
            LocalDate date2 = LocalDate.parse(stockGatherVo.getDate()[i], formatter);
            if (startDate.isBefore(date2) || startDate.equals(date2)) {
                // 日期
                String[] newArr = new String[stockGatherVo.getDate().length - i];
                System.arraycopy(stockGatherVo.getDate(), i, newArr, 0, newArr.length);
                stockGatherVo.setDate(newArr);

                double[][] newK = new double[stockGatherVo.getK().length - i][];
                System.arraycopy(stockGatherVo.getK(), i, newK, 0, newK.length);
                stockGatherVo.setK(newK);

                double[] newDif = new double[stockGatherVo.getDif().length - i];
                System.arraycopy(stockGatherVo.getDif(), i, newDif, 0, newDif.length);
                stockGatherVo.setDif(newDif);

                double[] newDea = new double[stockGatherVo.getDea().length - i];
                System.arraycopy(stockGatherVo.getDea(), i, newDea, 0, newDea.length);
                stockGatherVo.setDea(newDea);

                final double[] newMacd = new double[stockGatherVo.getMacd().length - i];
                System.arraycopy(stockGatherVo.getMacd(), i, newMacd, 0, newMacd.length);
                stockGatherVo.setMacd(newMacd);
                stockGatherVo.getKdj().subList(0, i).clear();
                stockGatherVo.getCci().subList(0, i).clear();
                stockGatherVo.getMa5().subList(0, i).clear();
                stockGatherVo.getMa10().subList(0, i).clear();
                stockGatherVo.getMa20().subList(0, i).clear();
                stockGatherVo.getMa30().subList(0, i).clear();
                break;
            }
        }


    }

    public static StockGatherVo adjustmentStockGatherVo(StockGatherVo stockGatherVo) {
        stockGatherVo.setDate(removeFirstElement(stockGatherVo.getDate()));
        stockGatherVo.setDif(removeFirstElement(stockGatherVo.getDif()));
        stockGatherVo.setDea(removeFirstElement(stockGatherVo.getDea()));
        stockGatherVo.setMacd(removeFirstElement(stockGatherVo.getMacd()));
        if(stockGatherVo.getKdj() != null && !stockGatherVo.getKdj().isEmpty()){
            stockGatherVo.getKdj().remove(0);
        }
        if(stockGatherVo.getCci() != null && !stockGatherVo.getCci().isEmpty()){
            stockGatherVo.getCci().remove(0);
        }
        stockGatherVo.getMa5().remove(0);
        stockGatherVo.getMa10().remove(0);
        stockGatherVo.getMa20().remove(0);
        stockGatherVo.getMa30().remove(0);

        double[][] newK = new double[stockGatherVo.getK().length - 1][];
        // 复制除了第一行之外的所有行到新数组
        // 注意索引从1开始，因为新数组少了一行
        System.arraycopy(stockGatherVo.getK(), 1, newK, 0, stockGatherVo.getK().length - 1);
        stockGatherVo.setK(newK);

        return stockGatherVo;
    }
    /**
     * 根据股票的历史数据，计算并返回一个包含多种技术指标的股票分析汇总对象。
     *
     * @param codeHistoryList 股票的历史数据列表，包含价格、成交量等信息。
     * @return 返回一个StockGatherVo对象，其中包含了计算得到的MACD、K线、均线、KDJ和CCI等技术指标。
     */
    public static StockGatherVo getStockGather(List<CodeHistory> codeHistoryList) {
        if(codeHistoryList == null || codeHistoryList.isEmpty()){
            return null;
        }
        // 计算MACD指标及其对应的日期
        // 获取macd
        StockGatherVo stockGatherVo = MacdUtils.calculateMacdAndDate(codeHistoryList);

        // 计算并设置K线信息
        // 获取k线
        stockGatherVo.setK(getKLine(codeHistoryList));

        // 计算并设置5日、10日、20日和30日均线
        // 获取均线
        stockGatherVo.setMa5(MACalculator.calculateMovingAverages(codeHistoryList, 5));
        stockGatherVo.setMa10(MACalculator.calculateMovingAverages(codeHistoryList, 10));
        stockGatherVo.setMa20(MACalculator.calculateMovingAverages(codeHistoryList, 20));
        stockGatherVo.setMa30(MACalculator.calculateMovingAverages(codeHistoryList, 30));

        // 计算并设置KDJ指标
        // 获取KDJ
        stockGatherVo.setKdj(CalculateKDJ.getKDJ(stockGatherVo));
        // 获取 CCi
        stockGatherVo.setCci(CCICalculator.calculateCCI(codeHistoryList));
        // 返回包含多种技术指标的股票分析汇总对象
        return stockGatherVo;
    }




    public static double[] removeFirstElement(double[] arr) {
        // 创建一个新的数组，长度比原数组少1
        double[] newArr = new double[arr.length - 1];
        System.arraycopy(arr, 1, newArr, 0, newArr.length);
        return newArr;
    }

    public static String[] removeFirstElement(String[] arr) {
        // 创建一个新的数组，长度比原数组少1
        String[] newArr = new String[arr.length - 1];
        System.arraycopy(arr, 1, newArr, 0, newArr.length);
        return newArr;
    }

    /**
     * 获取K线
     *
     * @param codeList 股票历史数据
     * @return K线
     */
    static double[][] getKLine(List<CodeHistory> codeList) {
        // 计算需要多少行
        int rowCount = codeList.size();
        // 创建一个二维数组，每行有4个元素
        double[][] k = new double[rowCount][4];

        for (int i = 0; i < rowCount; i++) {
            CodeHistory code = codeList.get(i);
            double[] v = new double[4];
            v[0] = code.getO().doubleValue();
            v[1] = code.getC().doubleValue();
            v[2] = code.getL().doubleValue();
            v[3] = code.getH().doubleValue();

            // 将每行的数据赋值给二维数组
            System.arraycopy(v, 0, k[i], 0, 4);
        }

        return k;
    }
}
